// 手动实现自动执行所有的测试脚本 *.spec.js
// 1. 获取到所有的测试版本 *.spec.js
// 2. 执行这些脚本

// import glob from 'glob'
import {glob} from 'glob'

import fs from 'fs/promises'
import { build } from 'esbuild'

const files = glob.sync("*.spec.js");  

for (const file of files) {
    const fileContent = await fs.readFile(file,"utf-8")
    await runModule(fileContent + ";import { run } from './core.js';run()")   
} 

async function runModule(fileContent) {
    const result = await build({
        stdin: {
            contents:fileContent,
            resolveDir:process.cwd()
        },
        write:false,
        bundle:true,
        target:"esnext"
    })
    // 获取测试代码
    // console.log(result.outputFiles[0].text);
    new Function(result.outputFiles[0].text)()
}
